package com.demarc.pesquisas.infraestrutura.util.relatorio;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import org.joda.time.LocalTime;

import com.demarc.pesquisas.infraestrutura.util.utils.DataUtils;

/**
 * A report column that holds the valuable object to be printed.
 */
public class ReportColumn {

	private final Report report;
	private final Object value;
	private ReportColumnHorizontalAlignment columnAlignment = ReportColumnHorizontalAlignment.CENTER;

	public ReportColumn(Report report, Object value) {
		this.report = report;
		this.value = value;
	}

	public Object getValue() {
		return value;
	}

	public ReportColumnHorizontalAlignment getColumnAlignment() {
		return columnAlignment;
	}

	public void setColumnAlignment(
			ReportColumnHorizontalAlignment columnAlignment) {
		this.columnAlignment = columnAlignment;
	}

	public String getAsString() {
		String result = "";

		// String
		if (value instanceof String) {
			result = (String) value;
		}

		// Boolean
		else if (value instanceof Boolean) {
			result = getBooleanAsString();
		}

		// LocalDate
		else if (value instanceof LocalDate) {
			result = getLocalDateAsString();
		}

		// LocalTime
		else if (value instanceof LocalTime) {
			result = getLocalTimeAsString();
		}

		// DateTime
		else if (value instanceof DateTime) {
			result = getDateTimeAsString();
		}

		// Other Class
		else if (value != null) {
			result = value.toString();
		}
		return result;
	}

	private String getDateTimeAsString() {
		String result;
		if (report.getLocale().equals(Report.LOCALE_BRAZIL)) {
			result = ((DateTime) value).toString("dd/MM/YYYY HH:mm",
					report.getLocale());
		} else {
			result = ((DateTime) value).toString("MM/dd/YYYY HH:mm",
					report.getLocale());
		}
		return result;
	}

	private String getLocalTimeAsString() {
		String result;
		if (report.getLocale().equals(Report.LOCALE_BRAZIL)) {
			result = ((LocalTime) value).toString("HH:mm", report.getLocale());
		} else {
			result = ((LocalTime) value).toString("HH:mm", report.getLocale());
		}
		return result;
	}

	private String getLocalDateAsString() {
		String padraoData = DataUtils.getPadraoData(Report.LOCALE_BRAZIL);
		return ((LocalDate) value).toString(padraoData, report.getLocale());
	}

	private String getBooleanAsString() {
		return Boolean.TRUE.equals(value) ? "Sim" : "Não";
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String toString() {
		return getAsString();
	}
}
